﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using Model;
using Model.EntityModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for ViewComplaintPart2.xaml
    /// </summary>
    public partial class ViewComplaintPart2 : UserControl
    {
        public long ComplaintId { get; set; }

        private List<FunctionaryInfo> EmployeesToAdd;
        public ViewComplaintContent ViewContentPrev { get; set; }

        public ViewComplaintPart2(ViewComplaintContent viewComplaintPrev)
        {
            InitializeComponent();
            ViewContentPrev = viewComplaintPrev;
            lblCaseNumber.Content = ViewContentPrev.lblCaseNumber.Content;
            lblRegistrationDate.Content = ViewContentPrev.lblCaseDate.Content;
            tbIncidentDetail.AddValitationRule(new ShortStringRule(50));
            ComplaintId = long.Parse(lblCaseNumber.Content.ToString());
            EmployeesToAdd = new List<FunctionaryInfo>();
            listBoxIntervinents.DataContext = EmployeesToAdd;
            LoadInformation();
        }

        private void LoadInformation()
        {
            var evidences = ComplaintDataAccess.GetAllEvidencesFromComplaint(ComplaintId);
            foreach (var evidence in evidences)
            {
                listBoxEvidences.Items.Add(evidence);
            }
            listBoxEvidences.UpdateLayout();
            tbIncidentDetail.Text = ComplaintDataAccess.GetComplaintById(ComplaintId).IncidentDetail;
            var employees = ComplaintDataAccess.GetAllEmployeeOfComplaintFromComp(ComplaintId);
            foreach (var employeeOfComplaint in employees)
            {
                if (employeeOfComplaint.Responsability == "Interviniente")
                {
                    var intervinent = EmployeeDataAccess.GetEmployeeByCi(employeeOfComplaint.CIEmployee);
                    EmployeesToAdd.Add(new FunctionaryInfo
                                                 {
                                                     CI = intervinent.CI,
                                                     Name =
                                                         intervinent.Name +" "+ intervinent.PaternalName +" "+
                                                         intervinent.MaternalName,
                                                     Rank = intervinent.Rank,
                                                     WorkingRole = intervinent.WorkingRole
                                                 });
                }
                if (employeeOfComplaint.Responsability == "Recepcionista")
                {
                    var recepter = EmployeeDataAccess.GetEmployeeByCi(employeeOfComplaint.CIEmployee);
                    lblRecepterName.Content = recepter.Name + " " + recepter.PaternalName + " " +
                                              recepter.MaternalName;
                    lblRecepterCi.Content = recepter.CI;
                    RecepterInfo.Visibility = Visibility.Visible;
                }
                if (employeeOfComplaint.Responsability == "Asignada")
                {
                    var asignedFuncionary = EmployeeDataAccess.GetEmployeeByCi(employeeOfComplaint.CIEmployee);
                    lblAsignedName.Content = asignedFuncionary.Name + " " + asignedFuncionary.PaternalName + " " +
                                              asignedFuncionary.MaternalName;
                    lblAsignedCi.Content = asignedFuncionary.CI;
                    AsignedInfo.Visibility = Visibility.Visible;
                }
                if (employeeOfComplaint.Responsability == "Jefe de Seguridad")
                {
                    var secBossFuncionary = EmployeeDataAccess.GetEmployeeByCi(employeeOfComplaint.CIEmployee);
                    lblSecBossName.Content = secBossFuncionary.Name + " " + secBossFuncionary.PaternalName + " " +
                                              secBossFuncionary.MaternalName;
                    lblSecBossCi.Content = secBossFuncionary.CI;
                    SecurityBossInfo.Visibility = Visibility.Visible;
                }
            }
            listBoxIntervinents.ItemsSource = EmployeesToAdd;
        }


        private void SaveAndContinue_Click(object sender, RoutedEventArgs e)
        {
            ViewContentPrev.viewComplaintpart2 = this;
            var par = this.Parent as ItemsControl;
            par.Items.Clear();
            par.Items.Add(ViewContentPrev);
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            DrpDownPrintOptions.IsOpen = true;
        }

        private void PrintPreview_Click(object sender, RoutedEventArgs e)
        {
            var items = new ObservableCollection<UserControl>();
            items.Add(new PrintComplaint(ComplaintId));
            items.Add(new PrintComplaintPart2(ComplaintId));
            items.Add(new PrintComplaintPart3(ComplaintId));
            var a = new PrintPreviewDialogV2(items);
            a.ShowDialog();
        }

        private void Print_Click(object sender, RoutedEventArgs e)
        {
            //PrintDialog pd = new PrintDialog();
            //if ((bool)pd.ShowDialog().GetValueOrDefault())
            //{
                var items = new ObservableCollection<UserControl>();
                items.Add(new PrintComplaint(ComplaintId));
                items.Add(new PrintComplaintPart2(ComplaintId));
                items.Add(new PrintComplaintPart3(ComplaintId));
                var a = new PrintPreviewDialogV2(items);
                a.MenuItem_Click(null,null);
                //pd.PrintVisual(new PrintComplaint(ComplaintId),"Complaint Page 1");
                //pd.PrintVisual(new PrintComplaintPart2(ComplaintId), "Complaint Page 2");
                //pd.PrintVisual(new PrintComplaintPart3(ComplaintId), "Complaint Page 3");
            //}
        }
    }
}
